# 对任意两个数字整理之后再求和
def sum_num(a, b):
    return abs(a) + abs(b)


# 高阶函数的实现
# def sum_num2(a, b, f):
#     """
#
#     :param a:
#     :param b:
#     :param f: 就是两个数字进行整理的函数
#     :return:
#     """
#     return f(a) + f(b)
#
#
# result = sum_num2(-12, 3, abs)
# print(result)
#
# result2 = sum_num2(2, 12, lambda n: n ** 2)
# print(result2)
#
#
# # 第二种高阶函数
#
# def test3(*args):
#     def sum_nums():
#         sum = 0
#         for n in args:
#             sum += n
#         return sum
#
#     return sum_nums
#
# print(test3(1,2,3)())


# 内置高阶函数
# map
# list_num = [1, 2, 3, 4, 5, 6]
# list_map = list(map(lambda n: n ** 2, list_num))
# print(list_map)
#
# # reduce函数
# from functools import reduce
#
# print(reduce(lambda x, y: x + y, list_map))

# 统计字符串里面出现的次数

# str1 = 'student hello word world hello name age name flask'
# lst = str1.split(' ')
# new_lst = list(map(lambda item: {item: 1}, lst))
# print(new_lst)
# from functools import reduce
#
# def func(dict1, dict2):
#     # 把dict1作为叠加的返回字典
#     key = list(dict2.items())[0][0]  # 得到dict2中的key
#     value = list(dict2.items())[0][1]  # 得到dict2中的key
#     dict1[key] = dict1.get(key, 0) + value
#     return dict1
#
#
# print(reduce(func, map(lambda item: {item: 1}, str1.split(' '))))


# filter
# lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# lst1 = list(filter(lambda n: n % 2, lst))
# print(lst1)

# sorted
lst = [
    {'name': 'xiaoming', 'age': 1},
    {'name': 'aa', 'age': 20},
    {'name': 'bb', 'age': 15},
]
# 根据年龄来排序
result = sorted(lst, key=lambda item: item['age'],reverse=True)
print(result)
